var rootPath = getRootPath();

$(function() {
	$("#password,#password2").focus(function() {
		hideMsg();
	});
	
	$("#password,#password2").bind('input propertychange',function() {
		if ($("#password").val() == '' || $("#password2").val() == ''
			|| $.trim($("#password").val()).length < 6 || $.trim($("#password").val()).length > 16
			|| $("#password").val() != $("#password2").val()) {
			$("#save").css({"background" : "#bbbbbb","cursor" : "default"});
			$("#save").attr("disabled", true);
		} else {
			$("#save").css({"background" : "rgb(197,24,10)","cursor" : "pointer"});
			$("#save").attr("disabled", false);
		}
	});

	$("#password").blur(function() {
		if($.trim($(this).val()) == '') {
			showErrMsg("请输入新密码");
			return;
		} else if($.trim($(this).val()).length < 6 || $.trim($(this).val()).length > 16) {
			showErrMsg("密码长度无效,应为6-16位");
			return;
		}
		var pattern = /\s+/;
		if(pattern.test($(this).val())) {
			showErrMsg("密码包含空格,请重新输入");
			return;
		}
		if($("#password2").val() != "" && $("#password2").val() != $(this).val()) {
			showErrMsg("密码不一致,请重新输入");
			return;
		}
	});
	
	$("#password2").blur(function() {
		if($(this).val() == '') {
			showErrMsg("请再次输入新密码");
			return;
		} else if($.trim($("#password").val()).length < 6 || $.trim($("#password").val()).length > 16) {
			showErrMsg("密码长度无效,应为6-16位");
			return;
		}
		var pattern = /\s+/;
		if(pattern.test($(this).val())) {
			showErrMsg("密码包含空格,请重新输入");
			return;
		}
		if($("#password").val() != $(this).val()) {
			showErrMsg("密码不一致,请重新输入");
			return;
		}
	});
	
	$("#save").click(function() {
		if ($.trim($("#password").val()) == "" || $.trim($("#password2").val()) == "") {
			showErrMsg("请输入新密码");
			return;
		}
		
		var pattern = /\s+/;
		if(pattern.test($("#password").val()) || pattern.test($("#password2").val())) {
			showErrMsg("密码包含空格,请重新输入");
			return;
		}

		if ($("#password").val() != $("#password2").val()) {
			showErrMsg("密码不一致,请重新输入:)");
			return;
		}

		showLoading();
		$.ajax({
			url : rootPath + '/loginFront/updateLogin',
			type : 'POST',
			data : JSON.stringify($("#setNewPwdForm").serializeJson()),
			contentType : 'application/json;charset=UTF-8',
			success : function(data) {
				hideLoading();
				if (data.returnCode == '0000') {
					layer.msg("更新成功", {icon : 1,time : 1000}, function() {
						parent.$("#content").prop("src","securitySetting.html");
					});
				} else {
					hideLoading();
					layer.alert(data.returnMessage);
				}
			},
			error : function(jqXHR, textStatus, errorThrown) {
				layer.alert("error");
			}
		});
	});
});

function showErrMsg(msg){
	$("#pwd_msg").text(msg);
	$(".msg").show();
}

function hideMsg() {
	$("#pwd_msg").text("");
	$(".msg").hide();
}